package com.qc.sys.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.qc.sys.common.Constant;
import com.qc.sys.common.DataGridView;
import com.qc.sys.common.ResultObj;
import com.qc.sys.entity.SysDept;
import com.qc.sys.service.SysDeptService;
import com.qc.sys.vo.DeptVo;
import io.swagger.models.auth.In;
import org.apache.ibatis.annotations.Delete;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * (SysDept)表控制层
 *
 * @author AltmanQccc
 * @since 2020-08-29 14:02:25
 */
@RestController
@RequestMapping("dept")
public class SysDeptController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private SysDeptService deptService;

    /**
     * 查询部门
     * */
    @GetMapping("loadAllDept")
    public Object loadAllDept(DeptVo deptVo){
        return this.deptService.queryAllDept(deptVo);

    }

    /**
     * 查询部门最大的排序码
     * */
    @GetMapping("queryDeptMaxOrderNum")
    public Object queryDeptMaxOrderNum(){
        Integer maxOrderNum = deptService.queryDeptMaxOrderNum();
        return new DataGridView(maxOrderNum+1);
    }

    /**
     * 添加部门
     * */
    @PostMapping("addDept")
    @RequiresPermissions("dept:add")
    public ResultObj addDept(SysDept dept){
        try {
            dept.setSpread(Constant.SPREAD_FALSE);
            dept.setAvailable(Constant.USER_AVAILABLE_TRUE);
            dept.setCreatetime(new Date());
            this.deptService.saveDept(dept);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 根据部门id去查询部门
     * */
    @GetMapping("getDeptById")
    public Object getDeptById(Integer id){
        return new DataGridView(deptService.getById(id));
    }

    /**
     * 修改部门
     * */
    @PostMapping("updateDept")
    @RequiresPermissions("dept:update")
    public ResultObj updateDept(SysDept dept){
        try {
            this.deptService.updateDept(dept);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 根据id查询当前部门是否有子部门
     * */
    @GetMapping("getDeptChildrenCountById")
    public Object getDeptChildrenCountById(Integer id){
        Integer count = this.deptService.queryDeptChildrenCountById(id);
        return new DataGridView(count);
    }

    /**
     * 根据id删除部门
     * */
    @PostMapping("deleteDept")
    @RequiresPermissions("dept:delete")
    public ResultObj deleteDept(Integer id){
        try {
            this.deptService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }
}